package com.ddwl.shop.controller;

import com.ddwl.common.annotation.Auth;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.constant.ShopEnum;
import com.ddwl.common.controller.BaseController;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.shop.MerchantBo;
import com.ddwl.schema.bo.shop.MerchantSearchBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.base.RestVo;
import com.ddwl.schema.vo.shop.MerchantVo;
import com.ddwl.shop.service.MerchantService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author Fu Zhaohui
 * @version 1.0.0
 * @date 2019/8/12 18:51
 */
@Api(tags = "商户服务")
@RequestMapping("/merchant")
@Auth(product = BaseEnum.ProductEnum.CONSOLE)
@RestController
@RequiredArgsConstructor
public class MerchantController extends BaseController {

    private final MerchantService merchantService;

    @PostMapping("/create")
    @ApiOperation("创建商户")
    public RestVo<Boolean> create(@Valid @RequestBody MerchantBo bo) {
        fillBaseField(bo);
        merchantService.create(bo);
        return RestVo.ok();
    }

    @PostMapping("/update")
    @ApiOperation("修改商户")
    public RestVo<Boolean> update(@Valid @RequestBody MerchantBo bo) {
        fillBaseField(bo);
        merchantService.update(bo);
        return RestVo.ok();
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除商户")
    public RestVo<Boolean> delete(@RequestParam String id) {
        merchantService.delete(id);
        return RestVo.ok();
    }

    @GetMapping("/detail")
    @ApiOperation("商户详情")
    public RestVo<MerchantVo> create(@RequestParam String id) {
        return RestVo.ok(merchantService.detail(id));
    }

    @PostMapping("/page")
    @ApiOperation("分页查询商户")
    public RestVo<PageVo<MerchantVo>> page(@ApiParam(value = "分页Bo", required = true) @Valid @RequestBody PageBo<MerchantSearchBo> pageBo) {
        return RestVo.ok(merchantService.page(pageBo));
    }

    @PostMapping("/search")
    @ApiOperation("搜索商户")
    public RestVo<List<MerchantVo>> search(@RequestBody MerchantSearchBo bo) {
        bo.setAuditStatus(ShopEnum.MerchantAuditStatus.PASS.getCode());
        return RestVo.ok(merchantService.search(bo));
    }
}
